export let dateDiff = 0
const OldDate = window.Date

export const diffDate = (time) => {
    dateDiff = time - new OldDate().getTime()
}

Object.setPrototypeOf = Object.setPrototypeOf ||
    function (obj, proto) {
        obj.__proto__ = proto;

        return obj;
    };


export const initDate = () => {
    // delete window[Date]

    function MyDate() {
        // bind属于Function.prototype，接收的参数是：object, param1, params2...
        var dateInst = new (Function.prototype.bind.apply(OldDate, [OldDate].concat(Array.prototype.slice.call(arguments))))();

        // 更改原型指向，否则无法调用MyDate原型上的方法
        // ES6方案中，这里就是[[prototype]]这个隐式原型对象，在没有标准以前就是__proto__
        Object.setPrototypeOf(dateInst, MyDate.prototype);

        if (arguments.length === 0) {
            dateInst.setTime(dateInst.getTime() + dateDiff)
        }
        return dateInst;
    }

    MyDate.now = () => OldDate.now() + dateDiff

// 原型重新指回Date，否则根本无法算是继承
    Object.setPrototypeOf(MyDate.prototype, OldDate.prototype);

    // class Date extends OldDate {
    //     constructor() {
    //         super();
    //         // super.setTime(super.getTime() + dateDiff);
    //         console.log(1111)
    //     }
    //
    //     getTest() {
    //         return this.getTime();
    //     }
    // }

    window.Date = MyDate
}
